
package com.crowdfunding.circle.navigate.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.common.param.SessionUserInfo;
import com.crowdfunding.circle.navigate.entity.PlatformNavigateVO;
import com.crowdfunding.circle.navigate.param.PlatformNavigateParam;
import com.crowdfunding.circle.navigate.service.PlatformNavigateService;

/**
 * 平台导航Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class PlatformNavigateController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PlatformNavigateController.class);

    @Autowired
    private PlatformNavigateService navigateService;

    private final static String LIST_PAGE = "/navigate/navigate_list_ajax";

    private final static String UPDATE_PAGE = "/navigate/navigate_edit";

    private static final String ADMIN_LIST_PAGE = "/admin/navigate_manage";;
    
    @RequestMapping(value = "/admin/navigate/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PlatformNavigateVO navigateVO = new PlatformNavigateVO();
        resultMap.put("navigateVO", navigateVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/navigate/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PlatformNavigateVO navigateVO = navigateService.findById(id);
        resultMap.put("navigate", navigateVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/navigate/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(PlatformNavigateVO navigateVO) {
        
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            
            String validMsg = navigateVO.valid();
            if (StringUtils.isNotBlank(validMsg)) {
                resultMap.put("result", false);
                resultMap.put("message", validMsg);
                return JSONObject.toJSONString(resultMap);
            }
            
            Integer affectCount = 0;
            
            if (StringUtils.equals("--请选择省份--", navigateVO.getPlatformProvince())) {
                navigateVO.setPlatformProvince("");
            }
            if (StringUtils.equals("--请选择城市--", navigateVO.getPlatformCity())) {
                navigateVO.setPlatformCity("");
            }
            
            if (navigateVO != null && StringUtils.isNotBlank(navigateVO.getNavigateId())) {
                affectCount = navigateService.update(navigateVO);
            } else if (navigateVO != null) {
                navigateVO.setNavigateId(UUID.randomUUID().toString());
                navigateVO.setPublishUser(seUser.getUserId());
                navigateVO.setDataStatus(0);
                affectCount = navigateService.insert(navigateVO);
            }
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改平台导航成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改平台导航失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改平台导航出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改平台导航出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/admin/navigate/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = navigateService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除平台导航成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除平台导航失败");
            }
        } catch (Exception e) {
            logger.error("删除平台导航出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除平台导航出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/uncheck/navigate/list")
    public ModelAndView queryList(PlatformNavigateParam navigateParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<PlatformNavigateVO> dataList = navigateService.queryByPage(navigateParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", navigateParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/navigate/list")
    public ModelAndView queryAdminList(PlatformNavigateParam navigateParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<PlatformNavigateVO> dataList = navigateService.queryByPage(navigateParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", navigateParam);
        return new ModelAndView(ADMIN_LIST_PAGE, resultMap);
    }
}
